package com.gln.bloger.action.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.gln.bloger.action.BaseAction;
import com.gln.bloger.pojos.User;
import com.gln.bloger.pojos.Website;

public class UserBaseAction extends BaseAction{
	/**
	 * 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		Website website = getAuthWebsite(request);
		User user = getAuthUser(request);
		ActionMessages errors = new ActionMessages();
		if(user == null){
			
			errors.add("errors", new ActionMessage("errors.no.authority"));
			saveErrors(request, errors);
			return mapping.findForward("login");
			
		}else if( website == null){
			
			errors.add("errors", new ActionMessage("errors.no.website.selected"));
			saveErrors(request, errors);
			return mapping.findForward("chooseBlogger");
			
		}else if( !user.isOwner(website)){
			
			errors.add("errors", new ActionMessage("errors.no.authority"));
			saveErrors(request, errors);
			return mapping.findForward("login");
			
		}else{
			return super.execute(mapping, form, request, response);
		}
	}

}
